package com.eightywork.life.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Mtag;
import com.eightywork.life.util.AndroidUtil;
import com.google.gson.JsonArray;

public class GroupListAdapter extends ArrayAdapter<Mtag> {
	private LayoutInflater mInflater;
	private int mResource = R.layout.item_group;
	private Context context;
	private int displayWidth;
	private int imageSize;
	private ImageLoader imageLoader;

	public GroupListAdapter(Context context, List<Mtag> list) {
		super(context, 0, list);
		this.context = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		init();
	}

	private void init() {
		displayWidth = ((Activity) context).getWindowManager()
				.getDefaultDisplay().getWidth();
		int spaceWidth = AndroidUtil.dpToPx(16, context);
		imageSize = (displayWidth - 7 * spaceWidth) / 3;
		imageLoader = new ImageLoader(context);
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		View layout = convertView;
		if (layout == null) {
			layout = mInflater.inflate(mResource, parent, false);
		}
		Mtag object = getItem(position);
		View view = layout.findViewById(R.id.item_title);
		TextView tagname = (TextView) layout.findViewById(R.id.tagname);
		TextView talknum = (TextView) layout.findViewById(R.id.talknum);
		TextView membernum = (TextView) layout.findViewById(R.id.membernum);
		if (object.getIsmember()!=0){
			view.setBackgroundResource(R.drawable.item_title);
			int color = getContext().getResources().getColor(R.color.white);
			tagname.setTextColor(color);
			talknum.setTextColor(color);
			membernum.setTextColor(color);
		}else{
			view.setBackgroundResource(R.drawable.item_title1);
			int color = getContext().getResources().getColor(R.color.gray);
			tagname.setTextColor(color);
			talknum.setTextColor(color);
			membernum.setTextColor(color);
		}
		tagname.setText(object.getTagname());
		talknum.setText(object.getTalknum()+"贴");
		membernum.setText(object.getMembernum()+"人");

		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				imageSize, imageSize);
		ImageView imageView1 = (ImageView) layout.findViewById(R.id.imageView1);
		layoutParams.setMargins(0, 0, 16, 0);
		imageView1.setLayoutParams(layoutParams);
		ImageView imageView2 = (ImageView) layout.findViewById(R.id.imageView2);
		imageView2.setLayoutParams(layoutParams);
		ImageView imageView3 = (ImageView) layout.findViewById(R.id.imageView3);
		imageView3.setLayoutParams(layoutParams);
		JsonArray json = object.getTopimages();
		imageLoader.DisplayImage(json.get(0).getAsString(), imageView1, 0, 0, 0);
		imageLoader.DisplayImage(json.get(1).getAsString(), imageView2, 0, 0, 0);
		imageLoader.DisplayImage(json.get(2).getAsString(), imageView3, 0, 0, 0);
		return layout;
	}
}
